using System;
using System.Collections.Generic;
using System.Xml;
using System.Text;
using System.Data;
using Microsoft.SharePoint;

namespace FTI.SP.LADM.EnhancedContentQueryWebPart.Wrappers
{
    public class ListItemWrap
    {
        private const string docIconXmlPFRelativePath = @"\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\XML\DOCICON.XML";
        public Guid WebID;
        public Guid ListID;
        public SharePointItemType ItemType;
        public string WebUrl;
        public string ItemUrl
        {
            get
            {
                //string serverRelativeUrl = this.ListItem.Web.ServerRelativeUrl;
                string serverRelativeUrl = this.ListItem.Web.Url;

                return Microsoft.SharePoint.Utilities.SPEncode.UrlEncodeAsUrl(serverRelativeUrl + "/" + ListItem.Url);
            }
        }

        public int ItemID;
        public SPListItem ListItem;
        public string SiteTitle;
        public string ItemContentType
        {
            get
            {
                if (this.ListItem == null || this.ListItem.File == null)
                    return "";
                return this.ListItem.ContentType.Name;
            }
        }
        public string ItemContentTypeID
        {
            get
            {
                if (this.ListItem == null || this.ListItem.File == null)
                    return "";
                return this.ListItem.ContentType.Id.ToString();
            }
        }
        public string ItemExtension
        {
            get
            {
                if (this.ListItem != null && this.ListItem.File != null)
                {
                    return this.ListItem.File.Name.Substring(this.ListItem.File.Name.LastIndexOf(".") + 1);
                }
                else
                    return "";
            }
        }
        public string ItemIconUrl
        {
            get
            {
                switch (this.ItemType)
                {
                    case SharePointItemType.Document:
                    case SharePointItemType.ListItem:
                        if (this.ListItem != null && this.ListItem.File != null)
                        {
                            return this.ListItem.File.IconUrl;
                        }
                        else
                            return "blank.GIF";
                        break;
                    case SharePointItemType.Folder:
                        return "FOLDER16.GIF";
                    default:
                        return "blank.GIF";
                }
            }
        }
        public string ItemApplicationName
        {
            get
            {
                if (this.ListItem != null && this.ListItem.File != null)
                {
                    if (this.ListItem.File.ProgID != null)
                        return this.ListItem.File.ProgID;
                }

                return "";
            }
        }
        public string ItemOpenMethod
        {
            get
            {
                if (this.ListItem != null && this.ListItem.File != null)
                {
                    if (this.ListItem.File.ProgID != null)
                        return this.ListItem.File.ProgID;
                }

                return "";
            }
        }
        public int CheckedOut
        {
            get
            {
                if (this.ListItem != null && this.ListItem.File != null)
                {
                    if (this.ListItem.File.CheckOutStatus == SPFile.SPCheckOutStatus.None)
                    {
                        return 0;
                    }
                    else
                    {
                        return 1;
                    }
                }
                else
                    return 0;
            }
        }
        public int CheckedOutUserID
        {
            get
            {
                if (this.ListItem == null || this.ListItem.File == null)
                    return -1;
                if (this.ListItem.File.CheckOutStatus != SPFile.SPCheckOutStatus.None)
                {
                    return this.ListItem.File.CheckedOutBy.ID;
                }
                else
                {
                    return -1;
                }
            }
        }
        public string SendToLocationName
        {
            get
            {
                if (this.ListItem == null || this.ListItem.File == null)
                    return "";
                return this.ListItem.ParentList.SendToLocationName;
            }
        }
        public string FolderUrl
        {
            get
            {

                if (this.ListItem == null)
                    return "";
                if (this.ListItem.Url.IndexOf("/") > 1)
                {
                    string serverRelativeUrl = this.ListItem.Web.ServerRelativeUrl;
                    if (serverRelativeUrl.StartsWith("/"))
                        serverRelativeUrl = serverRelativeUrl.Substring(1);

                    return serverRelativeUrl + "/" + this.ListItem.Url.Substring(0, this.ListItem.Url.LastIndexOf("/"));
                }
                return "";
            }
        }
        public string DocType
        {
            get
            {
                if (ListItem.File != null)
                {
                    return ListItem.File.ProgID;
                }
                else
                {
                    return "";
                }
            }
        }
        public string CopySource
        {
            get
            {
                if (!string.IsNullOrEmpty(ListItem.CopySource))
                    return ListItem.CopySource;
                else
                    return "";
            }
        }
        public string ModerationStatus
        {
            get
            {                
                if (ListItem.ModerationInformation != null)
                {
                    return ((int)ListItem.ModerationInformation.Status).ToString();
                }
                else
                    return "0";
            }
        }

        public bool DeclaredRecord
        {
            get
            {
                return ListItem.Properties.Contains("_vti_ItemDeclaredRecord") ? true : false;               
            }
        }
        public string DocEditCommand
        {
            get
            {
                if (ItemExtension.Length > 0)
                {
                    //todo: get information from C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\XML\DOCICON.XML
                    try
                    {
                        string pathToFile = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + docIconXmlPFRelativePath;
                        XmlDocument docs = new XmlDocument();
                        docs.Load(pathToFile);
                        XmlNode docNode = docs.DocumentElement.SelectSingleNode("//Mapping[@Key='" + this.ItemExtension + "']");
                        if (docNode != null)
                        {
                            string editText = "";
                            if (docNode.Attributes["EditText"] != null)
                                editText = docNode.Attributes["EditText"].Value;

                            editText += "|";

                            if (docNode.Attributes["OpenControl"] != null)
                                editText += docNode.Attributes["OpenControl"].Value;

                            return editText;
                        }

                    }
                    catch { }
                    return "Registered Application|SharePoint.OpenDocuments";
                }
                else
                    return "";
            }
        }
        public ListItemWrap(DataRow dr, SPSite site)
        {
            try
            {
                this.WebID = new Guid(dr["WebId"].ToString());
                this.ListID = new Guid(dr["ListId"].ToString());
                this.ItemID = int.Parse(dr["ID"].ToString());
                using (SPWeb web = site.OpenWeb(this.WebID))
                {
                    this.SiteTitle = web.Title;
                    this.WebUrl = web.Url;
                    SPList list = web.Lists[this.ListID];
                    try
                    {
                        this.ListItem = list.Items.GetItemById(this.ItemID);
                        if (this.ListItem == null)
                        {
                            dr.Delete();
                        }
                        if (this.ListItem.File == null)
                            this.ItemType = SharePointItemType.ListItem;
                        else
                            this.ItemType = SharePointItemType.Document;
                    }
                    catch
                    {
                        this.ListItem = list.Folders.GetItemById(this.ItemID);
                        if (this.ListItem == null)
                        {
                            dr.Delete();
                        }
                        this.ItemType = SharePointItemType.Folder;
                    }
                }
            }
            catch
            {
            }
        }
    }
    public enum SharePointItemType
    {
        ListItem,
        Document,
        Folder,
        Library,
        List,
        Site
    }
}
